<?php
/*
* Copyright (c) 2010 by Cediti
*
* You may distribute under the terms of either the GNU Lesser General
* Public License or the new BSD License, as specified in the README file.
*/
// created :    09/02/2010
// version :    02/04/2010

class Cmis_Client_Services_Generic_Discovery extends Cmis_Services_Abstract implements Cmis_Services_Discovery
{
	public function query(Cmis_Repository_Id $repositoryId, $statement, $searchAllVersions = false, $includeRelationships = null, $renditionFilter = null, $includeAllowableActions = false, $maxItems = null, $skipCount = null)
	{
		//@see http://docs.oasis-open.org/cmis/CMIS/v1.0/cs01/cmis-spec-v1.0.html#_Toc243905422
		/* @var $cur Cmis_Repository_Info */
		$cur = $this->_getRepositoryInfo($repositoryId);

		switch ($cur->getCapabilities('Query'))
		{
			default:
			case Cmis_Repository_Capability_Query::VALUE_NONE :
				throw new Cmis_Exception_Services_Services_Unavailable('query');
				break;
			case Cmis_Repository_Capability_Query::VALUE_METADATAONLY :
				if (stristr($string, 'CONTAINS('))
				{
					throw new Cmis_Exception_Services_Services_Unavailable('query with contains');
				}
				break;
            case Cmis_Repository_Capability_Query::VALUE_BOTHCOMBINED :
            case Cmis_Repository_Capability_Query::VALUE_BOTHSEPARATE :
            case Cmis_Repository_Capability_Query::VALUE_FULLTEXTONLY :
            	//can check so ...ok :(
            	break;
		}

		if (stristr($string, ' JOIN '))
		{
			switch ($cur->getCapapbilities('Join'))
			{
				default:
				case Cmis_Repository_Capability_Join::VALUE_NONE :
					throw new Cmis_Exception_Services_Services_Unavailable('join');
					break;
				case Cmis_Repository_Capability_Join::VALUE_INNERANDOUTER :
					break;
				case Cmis_Repository_Capability_Join::VALUE_INNERONLY :
					if (preg_match('#(LEFT|OUTER) JOIN#si', $statement))
					{
						throw new Cmis_Exception_Services_Services_Unavailable('join with outer or left');
					}
					break;
			}
		}

		$response = $this->getServices()->getProtocolService('Discovery')->query($repositoryId, $statement, $searchAllVersions, $includeRelationships, $renditionFilter, $includeAllowableActions, $maxItems, $skipCount);
		if (empty($response))
		{
			return null;
		}

		$ret = array();
		$xml = $response->toXml();
		
//        Debug::active();
        $ret[] = new Cmis_Repository_Entry($xml, $repositoryId, true);
//        Debug::var_xml($xml->asXml());
//        Debug::var_debug(count($xml), $xml->xpath('//entry'));

        $entries = (in_array('atom', $xml->getDocNamespaces()))?$xml->xpath('//atom:entry'):$xml->xpath('//entry');
        //$txt = $response->getRaw(Cmis_Http_Response::RAW_BODY);
        //Debug::var_xml($txt);
        //Debug::var_debug(new SimpleXMLElement($txt));

        $prefix = (func_num_args() > 8)? func_get_arg(9):'';

        //entries
        foreach($entries as $entry)
        {
            $ret[] = new Cmis_Repository_Entry($entry, $repositoryId, false, $prefix);
        }

        return $ret;
	}

	public function getContentChanges()
	{
		die('-[Must be implemented : ' . __METHOD__ . ' (' . __FILE__ . '#' . __LINE__ . ')]-');
	}
}

# EOF
